<?php
/*
 * @Author: lokei
 * @Date: 2022-09-26 10:42:08
 * @LastEditors: lokei
 * @LastEditTime: 2022-12-12 21:44:21
 * @Description: 
 */
namespace App\Http\Controllers\Ucenter\User;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Ucenter\UserModel;
use Illuminate\Http\Request;

class UserController extends Controller {
    public function list(Request $request) {
        if($request->input('mobile') != null && $request->input('mobile') != '') {
            $model = UserModel::where('mobile', '=', $request->input('mobile'));
        }
        if($request->input('name') != null && $request->input('name') != '') {
            if(isset($model)) {
                $model->where('realname','LIKE',"%{$request->input('name')}%")->orWhere('nickname','LIKE',"%{$request->input('name')}%");
            } else {
                $model = UserModel::where('realname','LIKE',"%{$request->input('name')}%")->orWhere('nickname','LIKE',"%{$request->input('name')}%");
            }
        }
        if(isset($model)) {
            $items = $model->get();
        } else {
            $items = UserModel::get();
        }
        $data = array();
        foreach($items as $item) {
            array_push($data, [
                'uid' => $item->uid,
                'avatar' => $item->avatar,
                'realname' => $item->realname,
                'nickname' => $item->nickname,
                'mobile' => $item->mobile,
                'balance' => $item->balance,
                'integral' => $item->integral,
                'createtime' => $item->createtime
            ]);
        }
        $res = ResultTool::success();
        $res['data']['items'] = $data;
        $res['data']['total'] = count($data);
        return $res;
    }

    public function get(Request $request) {
        $user = UserModel::where('uid', '=', $request->input('uid'))->first();
        $res = ResultTool::success();
        $data = [
            'mobile' => $user->mobile
        ];
        $res['data'] = $data;
        return $res;
    }

    public function update(Request $request) {
        $is_other = UserModel::where('mobile', '=', $request->input('mobile'))->where('uid', '!=', $request->input('uid'))->first();
        if(!is_null($is_other)) {
            $res = ResultTool::fail();
            $res['errorMsg'] = '手机号已经绑定其它用户！';
            return $res;
        }
        $user = UserModel::where('uid', '=', $request->input('uid'))->first();
        $user->mobile = $request->input('mobile');
        $user->save();
        return ResultTool::success();
    }

    public function updateOfMe(Request $request) {
        $user = app('auth')->user();
        $user->nickname = $request->input('nickname');
        $user->avatar = $request->input('avatar');
        $user->save();
        return ResultTool::success();
    }

    public function delete(Request $request) {
        UserModel::where('uid', '=', $request->input('uid'))->delete();
        return ResultTool::success();
    }
}